/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jTFuncionario.java
 *
 * Created on 10/11/2009, 00:49:25
 */

package restaurante.forms;

import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import restaurante.dao.ProdutoDao;
import restaurante.logica.Produto;
import restaurante.logica.Refeicao;
import restaurante.dao.RefeicaoDao;
import restaurante.logica.RefeicaoProduto;
import restaurante.dao.RefeicaoProdutoDao;
import restaurante.logica.Venda;
import java.sql.Date;
import java.text.SimpleDateFormat;
import restaurante.logica.NotaFiscal;
import restaurante.dao.NotaFiscalDao;

/**
 *
 * @author Fernanda
 */
public class jFVenda extends javax.swing.JFrame {

    /** Creates new form jTFuncionario */
    public jFVenda(Refeicao r1) throws SQLException {
        
        initComponents();
        jTIdRefeicao.setText(String.valueOf(r1.getId()));
        jTValorTotal.setText(String.valueOf(r1.getValor_total()));

        Date data = new Date(System.currentTimeMillis());
        SimpleDateFormat formatarDate = new SimpleDateFormat("yyyy-MM-dd");

        jTData.setText(formatarDate.format(data)); 

    //    populaTabelaVenda("");
        //desabilitaDados();
        //desabilitarVisaoTodos();
    }

    DefaultTableModel tmVenda = new DefaultTableModel(
            new Object [][]{
            },
            new String[]{"quantidade","nome","tamanho","preco_unidade","total"});
    
    private List<Venda> vendas;
    ListSelectionModel lsmVendas;



    DefaultTableModel tmProduto = new DefaultTableModel(
            new Object [][]{
            },
            new String[]{"id", "nome","preco_venda","tamanho"});

    private List<Produto> produtos;
    ListSelectionModel lsmProduto;


    DefaultTableModel tmNotaFiscal = new DefaultTableModel(
            new Object [][]{
            },
            new String[]{"quantidade","nome","preco_unidade","total"});

    private List<Venda> notaFiscal;
    ListSelectionModel lsNotaFiscal;



    public void populaTabelaVenda() throws SQLException{

        for (Venda v1 : vendas) {
            insereTabelaVenda(v1);
        }
    }

    public void populaTabelaProduto(String busca) throws SQLException{

        ProdutoDao pd = new ProdutoDao();
        produtos = pd.getLista("%"+busca+"%");

        for (Produto p1 : produtos) {
            insereTabelaProduto(p1);
        }
    }

   public void populaTabelaNotaFiscal(){

        for (Venda nf : notaFiscal) {
            insereTabelaNotaFiscal(nf);
        }
    }

    public void insereTabelaVenda(Venda v1){

        tmVenda.addRow(new String[]{
            String.valueOf(v1.getQuantidade()),
            v1.getNome(),
            v1.getTamanho(),
            String.valueOf(v1.getPreco()),
            String.valueOf(v1.getTotal())
        });
    }


    public void insereTabelaProduto(Produto p1){

        tmProduto.addRow(new String[]{
            String.valueOf(p1.getId()),
            p1.getNome(),
            String.valueOf(p1.getPreco_venda()),
            String.valueOf(p1.getTamanho()),
        });
    }

    public void insereTabelaNotaFiscal(Venda v1){

        tmNotaFiscal.addRow(new String[]{
            String.valueOf(v1.getQuantidade()),
            v1.getNome(),
            String.valueOf(v1.getPreco()),
            String.valueOf(v1.getTotal())
        });
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPCadProduto = new javax.swing.JPanel();
        jPPesquisaNome = new javax.swing.JPanel();
        jTIdFuncionario = new javax.swing.JTextField();
        jLID = new javax.swing.JLabel();
        jLNome = new javax.swing.JLabel();
        jTCpfCliente = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTProdutosVenda = new javax.swing.JTable();
        jBExcluir = new javax.swing.JButton();
        jLMarca = new javax.swing.JLabel();
        jTValorTotal = new javax.swing.JTextField();
        jBNotaFiscal = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTIdRefeicao = new javax.swing.JTextField();
        jTData = new javax.swing.JTextField();
        jBGrava = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jBSair = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTQuantidade = new javax.swing.JTextField();
        jBAdicionar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTListaProdutos = new javax.swing.JTable();
        jLEstoque = new javax.swing.JLabel();
        jTBuscarProduto = new javax.swing.JTextField();
        jBPesquisa = new javax.swing.JButton();
        jPNotaFiscal = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        setBounds(new java.awt.Rectangle(354, 124, 600, 600));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(new java.awt.Color(255, 255, 255));
        setIconImages(null);
        setName(""); // NOI18N
        setUndecorated(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPCadProduto.setBackground(new java.awt.Color(102, 255, 204));
        jPCadProduto.setAlignmentX(0.0F);
        jPCadProduto.setAlignmentY(0.0F);
        jPCadProduto.setPreferredSize(new java.awt.Dimension(600, 600));

        jPPesquisaNome.setBackground(new java.awt.Color(255, 255, 255));
        jPPesquisaNome.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "VENDA", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18))); // NOI18N
        jPPesquisaNome.setPreferredSize(new java.awt.Dimension(580, 61));

        jLID.setText("ID Funcionário:");

        jLNome.setText("CPF Cliente");

        jTCpfCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTCpfClienteActionPerformed(evt);
            }
        });

        jTProdutosVenda.setModel(tmVenda);
        jTProdutosVenda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTProdutosVendaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTProdutosVenda);

        jBExcluir.setText("EXCLUIR ITEM");
        jBExcluir.setPreferredSize(new java.awt.Dimension(100, 25));
        jBExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBExcluirActionPerformed(evt);
            }
        });

        jLMarca.setText("VALOR TOTAL:");

        jTValorTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTValorTotalActionPerformed(evt);
            }
        });

        jBNotaFiscal.setText("NOTA FISCAL");
        jBNotaFiscal.setPreferredSize(new java.awt.Dimension(100, 25));
        jBNotaFiscal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNotaFiscalActionPerformed(evt);
            }
        });

        jLabel2.setText("ID Refeição");

        jTIdRefeicao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTIdRefeicaoActionPerformed(evt);
            }
        });

        jTData.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTDataActionPerformed(evt);
            }
        });

        jBGrava.setText("GRAVAR");
        jBGrava.setPreferredSize(new java.awt.Dimension(100, 25));
        jBGrava.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBGravaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPPesquisaNomeLayout = new javax.swing.GroupLayout(jPPesquisaNome);
        jPPesquisaNome.setLayout(jPPesquisaNomeLayout);
        jPPesquisaNomeLayout.setHorizontalGroup(
            jPPesquisaNomeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPPesquisaNomeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPPesquisaNomeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                    .addGroup(jPPesquisaNomeLayout.createSequentialGroup()
                        .addComponent(jLID)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTIdFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTCpfCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTIdRefeicao, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTData, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
                    .addGroup(jPPesquisaNomeLayout.createSequentialGroup()
                        .addComponent(jBExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBNotaFiscal, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBGrava, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLMarca)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTValorTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPPesquisaNomeLayout.setVerticalGroup(
            jPPesquisaNomeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPPesquisaNomeLayout.createSequentialGroup()
                .addGroup(jPPesquisaNomeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLID)
                    .addComponent(jTIdFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLNome)
                    .addComponent(jTCpfCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTIdRefeicao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(jPPesquisaNomeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTValorTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBNotaFiscal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBGrava, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLMarca)))
        );

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        jBSair.setText("SAIR");
        jBSair.setPreferredSize(new java.awt.Dimension(100, 50));
        jBSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSairActionPerformed(evt);
            }
        });

        jLabel1.setText("QUANTIDADE");

        jTQuantidade.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTQuantidade.setText("01");
        jTQuantidade.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTQuantidadeMouseClicked(evt);
            }
        });

        jBAdicionar.setText("OK");
        jBAdicionar.setPreferredSize(new java.awt.Dimension(100, 25));
        jBAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAdicionarActionPerformed(evt);
            }
        });

        jTListaProdutos.setModel(tmProduto);
        jTListaProdutos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTListaProdutosMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTListaProdutos);

        jLEstoque.setText("Buscar Produto");

        jTBuscarProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTBuscarProdutoActionPerformed(evt);
            }
        });

        jBPesquisa.setText("Pesquisa");
        jBPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPesquisaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLEstoque)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTBuscarProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBPesquisa))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 475, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTQuantidade, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .addComponent(jBAdicionar, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .addComponent(jBSair, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLEstoque)
                    .addComponent(jTBuscarProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBPesquisa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
                        .addComponent(jBSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPCadProdutoLayout = new javax.swing.GroupLayout(jPCadProduto);
        jPCadProduto.setLayout(jPCadProdutoLayout);
        jPCadProdutoLayout.setHorizontalGroup(
            jPCadProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPCadProdutoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPCadProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPPesquisaNome, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPCadProdutoLayout.setVerticalGroup(
            jPCadProdutoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPCadProdutoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPPesquisaNome, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(jPCadProduto, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
        jPCadProduto.getAccessibleContext().setAccessibleName("CadastroFuncionario");

        jTable1.setModel(tmNotaFiscal);
        jScrollPane3.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(61, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 476, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(63, 63, 63))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(187, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPNotaFiscalLayout = new javax.swing.GroupLayout(jPNotaFiscal);
        jPNotaFiscal.setLayout(jPNotaFiscalLayout);
        jPNotaFiscalLayout.setHorizontalGroup(
            jPNotaFiscalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPNotaFiscalLayout.setVerticalGroup(
            jPNotaFiscalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        getContentPane().add(jPNotaFiscal, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTCpfClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTCpfClienteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTCpfClienteActionPerformed

    private void jTValorTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTValorTotalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTValorTotalActionPerformed

    private void jBAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAdicionarActionPerformed

        int linha = jTListaProdutos.getSelectedRow();

        String temp;

        Venda v1 = new Venda();

            temp = String.valueOf(jTListaProdutos.getValueAt(linha, 0));
        v1.setId_produto(Integer.parseInt(temp));
        v1.setQuantidade(Integer.parseInt(jTQuantidade.getText()));
        v1.setNome(String.valueOf(jTListaProdutos.getValueAt(linha,1)));
        v1.setTamanho(String.valueOf(jTListaProdutos.getValueAt(linha, 3)));
            temp = String.valueOf(jTListaProdutos.getValueAt(linha, 2));
        v1.setPreco(Float.parseFloat(temp));
        v1.setTotal( v1.getQuantidade() * v1.getPreco());

        insereTabelaVenda(v1);

        RefeicaoProduto rp = new RefeicaoProduto();

        rp.setId_refeicao(Integer.parseInt(jTIdRefeicao.getText()));
        rp.setId_produto(v1.getId_produto());
        rp.setQuantidade(Integer.parseInt(jTQuantidade.getText()));

        adicinaRefeicaoProduto(rp);

        
        Float   total;
        total = v1.getTotal() + Float.parseFloat(jTValorTotal.getText());
        jTValorTotal.setText(String.valueOf(total));
        jTQuantidade.setText("01");


    }//GEN-LAST:event_jBAdicionarActionPerformed

    private void jBNotaFiscalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNotaFiscalActionPerformed



        NotaFiscal nf = new NotaFiscal();

        nf.setId_refeicao(Integer.parseInt(jTIdRefeicao.getText()));
        nf.setData(jTData.getText());
        nf.setCpf_cliente(jTCpfCliente.getText());

        try {
            NotaFiscalDao dao = new NotaFiscalDao();
            dao.adiciona(nf);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Falha na Gravação da Nota FIscal");
        }

        
        try {
            new jFNotaFiscal(String.valueOf(nf.getNumero())).setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(jFVenda.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jBNotaFiscalActionPerformed

    private void jBSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_jBSairActionPerformed

    private void jTProdutosVendaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTProdutosVendaMouseClicked

       // escolherLinha();
        // TODO add your handling code here:
    }//GEN-LAST:event_jTProdutosVendaMouseClicked

    private void jBExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBExcluirActionPerformed

        Refeicao r1 = new Refeicao();
        Venda v1 = new Venda();
        Integer     linha;
        Float       valor,valorTotal;
        
        linha = jTProdutosVenda.getSelectedRow();
        valor = Float.parseFloat(String.valueOf(jTProdutosVenda.getValueAt(linha, 4)));
        valorTotal = Float.parseFloat(jTValorTotal.getText());
        valorTotal = valorTotal - valor;
        jTValorTotal.setText(String.valueOf(valorTotal));
        
        tmVenda.removeRow(linha);

        r1.setId(Integer.parseInt(String.valueOf(jTIdRefeicao.getText())));
        r1.setValor_total(valorTotal - valor);

        atualizaRefeicao(r1);
        

    }//GEN-LAST:event_jBExcluirActionPerformed

    private void jBPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPesquisaActionPerformed

        tmProduto.setNumRows(0);
        try {
            populaTabelaProduto(jTBuscarProduto.getText());
        } catch (SQLException ex) {
            Logger.getLogger(jFVenda.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_jBPesquisaActionPerformed

    private void jTBuscarProdutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTBuscarProdutoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTBuscarProdutoActionPerformed

    private void jTListaProdutosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTListaProdutosMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jTListaProdutosMouseClicked

    private void jTDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTDataActionPerformed

        // TODO add your handling code here:
    }//GEN-LAST:event_jTDataActionPerformed

    private void jBGravaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBGravaActionPerformed

        Refeicao r1 = new Refeicao();

        r1.setId(Integer.parseInt(String.valueOf(jTIdRefeicao.getText())));
        r1.setValor_total(Float.parseFloat(String.valueOf(jTValorTotal.getText())));

        System.out.println(String.valueOf(jTIdRefeicao.getText()));
        System.out.println(String.valueOf(jTValorTotal.getText()));

        atualizaRefeicao(r1);


    }//GEN-LAST:event_jBGravaActionPerformed

    private void jTQuantidadeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTQuantidadeMouseClicked

        jTQuantidade.setText("");

    }//GEN-LAST:event_jTQuantidadeMouseClicked

    private void jTIdRefeicaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTIdRefeicaoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTIdRefeicaoActionPerformed

    private void adicinaRefeicaoProduto(RefeicaoProduto rp){
        
        try {
            RefeicaoProdutoDao dao = new RefeicaoProdutoDao();
            dao.adiciona(rp);
        } catch (SQLException ex) {
            Logger.getLogger(jFVenda.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void escolherLinha(){

        int linha = jTProdutosVenda.getSelectedRow();
        System.out.println(linha);

        Produto p1 = new Produto();
        String temp;

            temp = String.valueOf(jTProdutosVenda.getValueAt(linha, 0));
        p1.setId(Integer.parseInt(temp));
        p1.setNome(String.valueOf(jTProdutosVenda.getValueAt(linha,1)));
            temp = String.valueOf(jTProdutosVenda.getValueAt(linha, 2));
        p1.setPreco_venda(Float.parseFloat(temp));
        p1.setTamanho(String.valueOf(jTProdutosVenda.getValueAt(linha, 3)));

        escreverJTextField(p1);
        
    }

    private void escreverJTextField(Produto i1){

        System.out.println(i1);

        jTIdFuncionario.setEditable(true);
        jTIdFuncionario.setText(String.valueOf(i1.getId()));
        jTIdFuncionario.setEditable(false);

        jTCpfCliente.setText(i1.getNome());
        jTValorTotal.setText(String.valueOf(i1.getPreco_venda()));
        jTBuscarProduto.setText(i1.getTamanho());

    }
    

    private void atualizaRefeicao(Refeicao r1){

        try {
            RefeicaoDao dao = new RefeicaoDao();
            dao.altera(r1);
        } catch (SQLException ex) {
            Logger.getLogger(jFVenda.class.getName()).log(Level.SEVERE, null, ex);
        }
    }



    public boolean verificaDados(){

        if (!jTCpfCliente.getText().equals("")) {
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "Campo nome não preenchido");
            return false;
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAdicionar;
    private javax.swing.JButton jBExcluir;
    private javax.swing.JButton jBGrava;
    private javax.swing.JButton jBNotaFiscal;
    private javax.swing.JButton jBPesquisa;
    private javax.swing.JButton jBSair;
    private javax.swing.JLabel jLEstoque;
    private javax.swing.JLabel jLID;
    private javax.swing.JLabel jLMarca;
    private javax.swing.JLabel jLNome;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPCadProduto;
    private javax.swing.JPanel jPNotaFiscal;
    private javax.swing.JPanel jPPesquisaNome;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTBuscarProduto;
    private javax.swing.JTextField jTCpfCliente;
    private javax.swing.JTextField jTData;
    private javax.swing.JTextField jTIdFuncionario;
    private javax.swing.JTextField jTIdRefeicao;
    private javax.swing.JTable jTListaProdutos;
    private javax.swing.JTable jTProdutosVenda;
    private javax.swing.JTextField jTQuantidade;
    private javax.swing.JTextField jTValorTotal;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
